<?php

namespace App\Containers\AppSection\Pledge\Models\Properties;

use App\Containers\AppSection\File\Models\File;
use App\Containers\AppSection\Pledge\Models\Properties\Contracts\Lease;
use App\Containers\AppSection\Pledge\Models\Properties\Contracts\Ownership;
use App\Containers\AppSection\Pledge\Models\Properties\Contracts\Uses;
use App\Containers\AppSection\Pledge\Observers\Properties\BuildingObserver;
use App\Ship\Parents\Models\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;

/**
 * Здание
 * @package App\Containers\AppSection\Pledge\Models\Properties
 *
 * @property int $id Id
 * @property string $address Адрес
 * @property string $number_type Тип номера ['cadastral', 'conditional', 'inventory']
 * @property string $number Номер
 * @property string $total_area Общая площадь
 * @property string $floors_number Этажность
 * @property string $object_composition Состав объекта
 * @property string $other_information Иные сведения
 * @property string $contract_type Тип права
 * @property string $contract_id Id права
 *
 * @property Lease|Ownership|Uses $contract Право
 * @property File[] $plans Технический план
 * @property File[] $documents Документы
 * @property File[] $images Фото залога
 */
class Building extends Model
{
    use HasJsonRelationships;

    public $timestamps = false;
    protected $table = 'pledge_property_buildings';
    protected $casts = [
        'plans' => 'json',
        'documents' => 'json',
        'images' => 'json',
    ];

    protected $fillable = [
        'address',
        'number_type',
        'number',
        'total_area',
        'floors_number',
        'object_composition',
        'other_information',
        'contract_type',
        'contract_id',
        'plans',
        'documents',
        'images',
    ];

    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Building';

    protected static function boot()
    {
        parent::boot();
        self::observe(new BuildingObserver);
    }

    /**
     * Технический план
     * @return BelongsToJson Files
     */
    public function plans(): belongsToJson
    {
        return $this->belongsToJson(File::class, 'plans');
    }

    /**
     * Документы
     * @return BelongsToJson Files
     */
    public function documents(): belongsToJson
    {
        return $this->belongsToJson(File::class, 'documents');
    }

    /**
     * Фото залога
     * @return BelongsToJson Files
     */
    public function images(): belongsToJson
    {
        return $this->belongsToJson(File::class, 'images');
    }

    /**
     * Право
     * @return MorphTo Lease|Ownership|Uses
     */
    public function contract(): MorphTo
    {
        return $this->morphTo('contract');
    }
}
